<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
<head>
    <title>新闻管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/all.min.css">
    <style>
        .container {
            padding: 20px;
        }
        .top-bar {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .search-box {
            display: flex;
            align-items: center;
        }
        .search-box input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            width: 200px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
        }
        .btn-primary {
            background-color: #1ab394;
        }
        .btn-primary:hover {
            background-color: #18a689;
        }
        .btn-warning {
            background-color: #f8ac59;
        }
        .btn-warning:hover {
            background-color: #f7a54a;
        }
        .btn-danger {
            background-color: #ed5565;
        }
        .btn-danger:hover {
            background-color: #ec4758;
        }
        .btn-info {
            background-color: #23c6c8;
        }
        .btn-info:hover {
            background-color: #21b9bb;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e7eaec;
        }
        th {
            background-color: #f5f5f6;
            color: #333;
        }
        .status-badge {
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 12px;
            color: white;
        }
        .status-active {
            background-color: #1ab394;
        }
        .status-inactive {
            background-color: #ed5565;
        }
        .carousel-badge {
            padding: 3px 8px;
            border-radius: 3px;
            font-size: 12px;
            color: white;
        }
        .carousel-yes {
            background-color: #1c84c6;
        }
        .carousel-no {
            background-color: #cacaca;
        }
        .thumbnail {
            max-width: 100px;
            max-height: 60px;
            object-fit: cover;
        }
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
        }
        .pagination button {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ddd;
            background-color: white;
            cursor: pointer;
            border-radius: 3px;
        }
        .pagination button:hover:not(:disabled) {
            background-color: #f5f5f5;
            color:#1ab394;
        }
        .pagination button.active {
            background-color: #1ab394;
            color: white;
            border-color: #1ab394;
        }
        .pagination button:disabled {
            background-color: #f5f5f5;
            cursor: not-allowed;
        }
        .pagination-info {
            margin: 0 15px;
            color: #666;
        }
        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
        }
        .modal-content {
            position: relative;
            background-color: #fff;
            margin: 50px auto;
            padding: 20px;
            width: 80%;
            max-width: 800px;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        .modal-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }
        .close {
            font-size: 24px;
            font-weight: bold;
            color: #666;
            cursor: pointer;
            border: none;
            background: none;
        }
        .close:hover {
            color: #333;
        }
        .modal-body {
            max-height: 500px;
            overflow-y: auto;
            padding: 20px;
        }
        .comment-item {
            border-bottom: 1px solid #eee;
            padding: 15px 0;
        }
        .comment-item:last-child {
            border-bottom: none;
        }
        .comment-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 8px;
            font-size: 14px;
            color: #666;
        }
        .comment-header .username {
            font-weight: bold;
            color: #333;
            margin-right: 10px;
        }
        .comment-header .time {
            color: #999;
        }
        .btn-delete {
            background: none;
            border: none;
            color: #ed5565;
            cursor: pointer;
            padding: 4px 8px;
            font-size: 14px;
            border-radius: 3px;
        }
        .btn-delete:hover {
            background-color: #fff1f1;
        }
        .comment-content {
            font-size: 14px;
            line-height: 1.5;
            color: #333;
            padding: 0 0 15px;
        }
        .no-comments {
            text-align: center;
            padding: 20px;
            color: #666;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="top-bar">
            <button class="btn btn-primary" onclick="location.href='${pageContext.request.contextPath}/admin/news/get'">
                <i class="fas fa-plus"></i> 添加新闻
            </button>
            <div class="search-box">
                <input type="text" id="searchInput" placeholder="输入新闻标题搜索" value="${title}">
                <button class="btn btn-primary" onclick="search()">
                    <i class="fas fa-search"></i> 搜索
                </button>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>图片</th>
                    <th>标题</th>
                    <th>分类</th>
                    <th>轮播图</th>
                    <th>状态</th>
                    <th>浏览次数</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${newsList}" var="news">
                    <tr>
                        <td>${news.newsId}</td>
                        <td>
                            <c:if test="${not empty news.imageUrl}">
                                <img src="${pageContext.request.contextPath}/${news.imageUrl}" 
                                     alt="${news.title}" class="thumbnail">
                            </c:if>
                        </td>
                        <td>${news.title}</td>
                        <td>${news.category}</td>
                        <td>
                            <span class="carousel-badge ${news.isCarousel == 1 ? 'carousel-yes' : 'carousel-no'}">
                                ${news.isCarousel == 1 ? '是' : '否'}
                            </span>
                        </td>
                        <td>
                            <span class="status-badge ${news.status == 1 ? 'status-active' : 'status-inactive'}">
                                ${news.status == 1 ? '上架' : '下架'}
                            </span>
                        </td>
                        <td>${news.viewCount}</td>
                        <td><fmt:formatDate value="${news.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                        <td>
                            <button class="btn btn-info" onclick="viewComments(${news.newsId}, '<c:out value="${news.title}" escapeXml="true"/>')">
                                <i class="fas fa-comments"></i> 评论
                            </button>
                            <button class="btn btn-warning" onclick="location.href='${pageContext.request.contextPath}/admin/news/get?id=${news.newsId}'">
                                <i class="fas fa-edit"></i> 编辑
                            </button>
                            <button class="btn btn-danger" onclick="deleteNews(${news.newsId})">
                                <i class="fas fa-trash"></i> 删除
                            </button>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>

        <div class="pagination">
            <button onclick="goToPage(${currentPage - 1})" ${currentPage <= 1 ? 'disabled' : ''}>
                <i class="fas fa-chevron-left"></i> 上一页
            </button>
            
            <c:forEach begin="1" end="${totalPages}" var="i">
                <c:choose>
                    <c:when test="${i == currentPage}">
                        <button class="active">${i}</button>
                    </c:when>
                    <c:otherwise>
                        <button onclick="goToPage(${i})">${i}</button>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            
            <button onclick="goToPage(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
                下一页 <i class="fas fa-chevron-right"></i>
            </button>
            
            <span class="pagination-info">
                共 ${totalCount} 条记录，${totalPages} 页
            </span>
        </div>
    </div>

    <!-- 评论模态框 -->
    <div id="commentModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title">新闻评论</h3>
                <button class="close" onclick="closeModal()">&times;</button>
            </div>
            <div class="modal-body" id="commentList">
                <!-- 评论内容将通过 JavaScript 动态加载 -->
            </div>
        </div>
    </div>

    <script>
        function deleteNews(id) {
            if (confirm('确定要删除这条新闻吗？')) {
                fetch('${pageContext.request.contextPath}/admin/news/delete?id=' + id, {
                    method: 'POST'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('删除成功');
                        location.reload();
                    } else {
                        alert(data.message || '删除失败');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('删除失败');
                });
            }
        }

        function search() {
            const searchValue = document.getElementById('searchInput').value.trim();
            location.href = '${pageContext.request.contextPath}/admin/news/list?title=' + 
                          encodeURIComponent(searchValue);
        }

        document.getElementById('searchInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                search();
            }
        });

        function goToPage(page) {
            const searchValue = document.getElementById('searchInput').value.trim();
            const searchParam = searchValue ? '&title=' + encodeURIComponent(searchValue) : '';
            location.href = '${pageContext.request.contextPath}/admin/news/list?page=' + page + searchParam;
        }

        // 查看评论相关函数
        function viewComments(newsId, newsTitle) {
            const modal = document.getElementById('commentModal');
            const commentList = document.getElementById('commentList');
            const modalTitle = document.querySelector('.modal-title');
            
            modalTitle.textContent = '评论列表 - ' + newsTitle;
            commentList.innerHTML = '<div style="text-align:center;padding:20px;">加载中...</div>';
            modal.style.display = 'block';

            // 获取评论数据
            fetch('${pageContext.request.contextPath}/admin/news/comments?newsId=' + newsId)
                .then(response => response.json())
                .then(data => {
                    if (data.success && data.comments) {
                        if (data.comments.length > 0) {
                            commentList.innerHTML = data.comments.map(comment => `
                                <div class="comment-item">
                                    <div class="comment-header">
                                        <div>
                                            <span class="username">\${comment.username || '匿名用户'}</span>
                                            <span class="time">\${formatDate(comment.createTime)}</span>
                                        </div>
                                        <button class="btn-delete" onclick="deleteComment(\${comment.commentId}, \${newsId})">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </div>
                                    <div class="comment-content">\${comment.content}</div>
                                </div>
                            `).join('');
                        } else {
                            commentList.innerHTML = '<div class="no-comments">暂无评论</div>';
                        }
                    } else {
                        commentList.innerHTML = '<div class="no-comments">加载评论失败</div>';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    commentList.innerHTML = '<div class="no-comments">加载评论失败</div>';
                });
        }

        function deleteComment(commentId, newsId) {
            if (confirm('确定要删除这条评论吗？')) {
                fetch('${pageContext.request.contextPath}/admin/news/comment/delete?id=' + commentId, {
                    method: 'POST'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // 重新加载评论列表
                        viewComments(newsId, document.querySelector('.modal-title').textContent.replace('评论列表 - ', ''));
                    } else {
                        alert(data.message || '删除失败');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('删除失败');
                });
            }
        }

        function closeModal() {
            document.getElementById('commentModal').style.display = 'none';
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('commentModal');
            if (event.target == modal) {
                modal.style.display = 'none';
            }
        }

        function formatDate(dateString) {
            if (!dateString) return '';
            const date = new Date(dateString);
            return date.getFullYear() + '-' + 
                   String(date.getMonth() + 1).padStart(2, '0') + '-' + 
                   String(date.getDate()).padStart(2, '0') + ' ' + 
                   String(date.getHours()).padStart(2, '0') + ':' + 
                   String(date.getMinutes()).padStart(2, '0') + ':' + 
                   String(date.getSeconds()).padStart(2, '0');
        }
    </script>
</body>
</html> 